from pygame.locals import *
import pygame.gfxdraw
from functions import *

def main():
    v_colorInit=150
    v_color=v_colorInit
    restart=True
    v_points=[]
    #v_pointsLength=len(v_points)
    v_index=len(v_points)-1
    pygame.init()
    screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
    pygame.display.set_caption('五子棋')

    font1 = pygame.font.SysFont('SimHei', 32)
    font2 = pygame.font.SysFont('SimHei', 72)
    fwidth, fheight = font2.size('黑方获胜')
    checkerboard = Checkerboard(Line_Points)
    cur_runner = BLACK_CHESSMAN
    winner = None
    computer = AI(Line_Points, WHITE_CHESSMAN)
    AI_Point=Point(-1,-1)
    black_win_count = 0
    white_win_count = 0
    v_loopGame = True  # hello name variable
    while v_loopGame:
        for event in pygame.event.get():
            if event.type == QUIT:
                v_loopGame = False
                # sys.exit()
            elif event.type == KEYDOWN:
                if event.key == K_RETURN:
                    if winner is not None:#如果分胜负了，则重新开局
                        winner = None
                        cur_runner = BLACK_CHESSMAN
                        checkerboard = Checkerboard(Line_Points)
                        computer = AI(Line_Points, WHITE_CHESSMAN)
                        restart=True
                        v_points.clear()
                        v_index=-1
            elif event.type == MOUSEBUTTONDOWN:
                pressed_array = pygame.mouse.get_pressed()
                if pressed_array[0]:
                    mouse_pos = pygame.mouse.get_pos()
                    click_point = get_clickpoint(mouse_pos)
                    if winner is None:
                        if click_point is not None:
                            if checkerboard.can_drop(click_point):
                                winner = checkerboard.drop(cur_runner, click_point)
                                restart=False
                                v_points.append(click_point)
                                v_index=len(v_points)-1
                                if winner is None:#如果玩家没有赢
                                    cur_runner = get_next(cur_runner)#黑换成白
                                    computer.get_opponent_drop(click_point)
                                    AI_Point = computer.AI_drop()
                                    v_points.append(AI_Point)
                                    v_index=len(v_points)-1
                                    winner = checkerboard.drop(cur_runner, AI_Point)
                                    if winner is not None:#如果电脑赢了
                                        white_win_count += 1
                                    cur_runner = get_next(cur_runner)
                                else:#如果玩家赢了
                                    black_win_count += 1
                        else:
                            print('超出棋盘区域')
                    if(mouse_pos[0]>590 and mouse_pos[0]<765 and mouse_pos[1]>188 and mouse_pos[1]<226):#重新开局
                        checkerboard= Checkerboard(Line_Points)
                        cur_runner= BLACK_CHESSMAN
                        winner= None
                        computer= AI(Line_Points, WHITE_CHESSMAN)
                        restart=True
                        v_points.clear()
                        v_index=-1
                    if(mouse_pos[0]>590 and mouse_pos[0]<705 and mouse_pos[1]>240 and mouse_pos[1]<280 and restart==False):#悔棋
                        if winner is None:
                            computer.AI_unDrop(v_points[v_index])
                            checkerboard.unDrop(v_points[v_index])
                            #v_points.append(v_points[v_index])
                            del v_points[v_index]
                            v_index-=1
                            checkerboard.unDrop(v_points[v_index])
                            computer.AI_unDrop(v_points[v_index])
                            del v_points[v_index]
                            v_index-=1
                        elif winner==BLACK_CHESSMAN:
                            checkerboard.unDrop(v_points[v_index])
                            computer.AI_unDrop(v_points[v_index])
                            del v_points[v_index]
                            v_index-=1
                            black_win_count-=1
                        else:
                            checkerboard.unDrop(v_points[v_index])
                            computer.AI_unDrop(v_points[v_index])
                            del v_points[v_index]
                            v_index-=1
                            checkerboard.unDrop(v_points[v_index])
                            computer.AI_unDrop(v_points[v_index])
                            del v_points[v_index]
                            v_index-=1
                            white_win_count-=1
                        winner= None
                        if len(v_points)==0:
                            restart=True
                    #print(mouse_pos)

        # 画棋盘
        draw_checkerboard(screen)

        # 画棋盘上已有的棋子
        for i, row in enumerate(checkerboard.checkerboard):#落子
            for j, cell in enumerate(row):
                if cell == BLACK_CHESSMAN.Value:
                    draw_chessman(screen, Point(j, i), BLACK_CHESSMAN.Color)
                elif cell == WHITE_CHESSMAN.Value:
                    draw_chessman(screen, Point(j, i), WHITE_CHESSMAN.Color)
        if(AI_Point.X>-1 and AI_Point.Y>-1 and not restart):#最后一颗白子的落子动画
            if winner is None:#cur_runner==BLACK_CHESSMAN:# and
                v_color+=1
                if v_color>255:
                    v_color=v_colorInit
                draw_chessman(screen, v_points[v_index], (v_color,v_color,v_color))
                #pygame.draw.circle(screen, (i,i,i), (Start_X + SIZE *AI_Point.X,Start_Y + SIZE * AI_Point.Y), Stone_Radius)

        drawRightInfo(screen, font1, cur_runner, black_win_count, white_win_count)

        if winner:
            print_text(screen, font2, (SCREEN_WIDTH - fwidth) // 2, (SCREEN_HEIGHT - fheight) // 2,
                       winner.Name + '获胜', RED_COLOR)
            #v_playGame=input("还想继续玩吗？")
            #main()

        pygame.display.flip()
    pygame.quit()


if __name__ == '__main__':
    main()
